<%@page import="java.sql.ResultSet" import="java.sql.Statement" import="java.sql.Connection" import="connection.DBConnection"%>
<%@ page import="java.util.*" language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@page import="java.text.SimpleDateFormat" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Debit Note Creation </title>

<script language="javascript" type="text/javascript">

function checkDate()
{
	var c1;
	
  	var datevalid=/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
  		
        c1= document.f122.dbdate.value;

        if(c1.length==0)
        {
        	alert("Enter Date");
            return false;
      	 }
        if(!c1.match(datevalid))
        {
            alert("Please Enter Date in DD/MM/YYYY Format");
            document.f122.dbdate.value="";
            document.f122.dbdate.focus();
            
	         return false;
    	 }
        else
        {
        	dateval=c1.match(datevalid);
        }
        
        var dtYear = dateval[1];        
        dtMonth = dateval[3];
        dtDay=  dateval[5];

     if (dtMonth < 1 || dtMonth > 12) 
        return false;
     else if (dtDay < 1 || dtDay> 31) 
       return false;
     else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31) 
       return false;
     else if (dtMonth == 2){ 
       var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
       if (dtDay> 29 || (dtDay ==29 && !isleap)) 
      	 
      	 alert("Date format is not correct");
       document.f14.rdate.value="";
       document.f14.rdate.focus();
              return false;
        
        return true;
     }
    
}
</script>

<script language="javascript" type="text/javascript">

function populateSupplierId(){
    var suppliername = document.getElementById('suppliername');

    /* selected value of dropdown */
    var selectedSupplierId = suppliername.options[suppliername.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('suppliercode').value = selectedSupplierId; 
}
</script>     

<script language="javascript" type="text/javascript">

function NetAmount()
{
    var p1 = document.getElementById("dbproductquantity").value;
    var p2 = document.getElementById("dbproductrate").value;
   
    var p3=parseInt(p1) * parseFloat(p2);
 
    document.getElementById("dbnetamount").value = p3; 
}
    
</script>     

<script language="javascript" type="text/javascript">

function TaxAmount(opt)
{
	var tax1 = document.getElementById("dbnetamount").value;
	var tax2 = opt.text;
	
	var tax3= (tax1 * tax2) /100;
	
	document.getElementById("dbtaxamount").value=tax3;
}

</script>

<script language="javascript" type="text/javascript">

function TotalAmount()
{
	var t1 = document.getElementById("dbnetamount").value;
	var t2 = document.getElementById("dbtaxamount").value;
	var t3 = document.getElementById("dbtransport").value;
	var t4 = document.getElementById("dbothercosts").value;
	
	var total=parseFloat(t1)+parseFloat(t2)+parseFloat(t3)+parseFloat(t4);
	
	document.getElementById("dbtotalamount").value = total;

}
	
</script>

</head>
<body>
<form name="f122" action="DebitNoteServlet">
<center><h2> Debit Note Creation </h2></center>
<table align="center">
<tr> <td> Against Purchase order number </td>
	  <td> <input type="text" name="dbordernum"/> </td>
</tr>

<tr>
	<td>Invoice number</td>
	
		<%	
				DBConnection dbc=new DBConnection();	
				Connection con=dbc.getNewConnection();
		
			 	Statement st = null;
			 	ResultSet rs = null;
			 	String code2="DB1";
			try
			{
			   st=con.createStatement() ;
			   //SELECT id FROM table1 WHERE scenedevice_id = X ORDER BY id DESC LIMIT 0,1
       		   rs=st.executeQuery("select MAX(dbinno) from DebitNote"); %>
       		   
       		 <%  while(rs.next()) 
       		 	{ 
       				String code1=rs.getString(1);
       				
       			   String[] part = code1.split("(?<=\\D)(?=\\d)");
	   		       
	   		       String p0=part[0];		       
	   		       int p1=Integer.parseInt(part[1]);
	   		      
	   		       p1++;
	   		       
	   		       code2=p0+""+p1;

            	} %>
  
	 		<% }
		   catch(Exception e){} %>
	
	<td><input type="text" name="dbinvoice" value="<%=code2 %>"/></td>
	<td>Date</td>
	
	<% SimpleDateFormat current = new SimpleDateFormat("dd/MM/yyyy"); %>	
	<td><input type="text" name="dbdate" value="<%=current.format(new Date()) %>" /></td>
</tr>

<tr>
	<td>Name of Supplier</td>

		
			<%	 
				 int i=1;
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select scode,sname from SupplierMaster"); 
       		   %>
	
		<td> 
	   		<select name="suppliername" id="suppliername" onchange="populateSupplierId();" >
	   			<option> </option>
	  
       		 <%  while(rs.next()){ %>
            		<option value="<%=rs.getString(1) %>" ><%= rs.getString(2)%></option>
            		
            		<% } %>
      		</select>
     	</td>      		
     
 
          <%    }
		catch(Exception e){} %>

	<td><a href="SupplierMaster.jsp"> Create New Supplier </a></td>

	<%-- <td><input type="submit" name="dbrefreshacc" value="Refresh account names"/> </td> --%>
</tr>

<tr>
	<td>Supplier code</td>
	<td><input type="text" name="suppliercode" id="suppliercode" />
	
<tr>
	<td>Product Name</td>
		
			 <%	 
			try
			{
			   st=con.createStatement() ;
       		   rs=st.executeQuery("select stname from StockMaster"); %>
	<td> 
	  <select name="stockname">
	  <option> </option>
       		 <%  while(rs.next()){ %>
            		<option><%= rs.getString(1)%></option>
      
          <% } %>
       </select>
     </td>
  
	 <% }
		catch(Exception e){ e.printStackTrace(); } %>
	
	<td><a href="StockMaster.jsp"> Create New Stock </a></td>
	<%-- <td><input type="submit" name="dbrefreshacc2" value="Refresh Item names"/></td> --%>
</tr>
	
<tr>
	<td>Product Quantity</td>
	<td><input type="text" name="dbproductquantity" id="dbproductquantity"/></td>
	<td>Product Rate</td>
	<td><input type="text" name="dbproductrate" id="dbproductrate" onblur="NetAmount();"/></td>
</tr>

<tr> 
	<td>Purchase Net Amount</td>
	<td><input type="text" name="dbnetamount" id="dbnetamount"/></td>
	<%-- <td><input type="button" name="dbcalculatenet" id="dbcalculatenet" value="Calculate Net Amount" onClick="NetAmount();" /></td> --%>
</tr>

<tr>
	<td>Taxation Category</td>
	<td><select name="dbtaxcat" id="dbtaxcat" onchange="TaxAmount((this.options[this.selectedIndex]));">
	<option> </option>
	<option >5.0</option>
	<option >12.5</option>
	</select></td>
</tr>
	
<tr>
	<td>Purchase Tax Amount</td>
	<td><input type="text" name="dbtaxamount" id="dbtaxamount"/></td>
	<%-- <td><input type="button" name="dbcalculatetax" value="Calculte Tax Amount" onClick="TaxAmount();"/></td> --%>
</tr>
	
<tr>
	<td>Purchase Transport Charges</td>
	<td> <input type="text" name="dbtransport" id="dbtransport" /></td>
	<td>Purchase Other Costs</td>
	<td><input type="text" name="dbothercosts" id="dbothercosts" onblur="TotalAmount();"/></td>
</tr>
	
<tr>
	<td>Purchase Total Amount</td>
	<td><input type="text" name="dbtotalamount" id="dbtotalamount"/></td>
	<%-- <td><input type="button" name="dbcalculatetotal" id="dbcalculatetotal" value="Calculate Total Amount" onClick="TotalAmount();"/></td> --%>
</tr>
	
<tr>
	<td>Credit limit (No. of Days)</td>
	<td><input type="text" name="dbcredlimit"/></td>
</tr>
<tr>
	<td> Reason for Rejection </td>
	<td> <input type="text" name="dbrejectionreason">
</tr>
<tr >
	<td><input type="submit" name="enter1" value="Enter"/></td>
</tr>

</table>
</form>
</body>
</html>